package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.aop.UserOperatePower;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.pojo.DeviceOperationDetailsVO;
import com.econ.powercloud.ems.pojo.DeviceOperationRecordVO;
import com.econ.powercloud.ems.pojo.ListingVO;
import com.econ.powercloud.ems.pojo.requestVO.DeviceCodesRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.DeviceListingRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.DeviceOperationSearchRequestVO;
import com.econ.powercloud.ems.service.biz.DeviceService;
import com.econ.powercloud.ems.service.biz.DeviceSyncMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jwc
 * @date 2022/07/21
 */
@Api(tags = "设备操作记录")
@RestController
@RequestMapping(value = "/api/v2/deviceOperation")
@RequiredArgsConstructor
public class DeviceOperationController extends ApiBaseController {
    private final DeviceSyncMsgService deviceSyncMsgService;
    private final DeviceService deviceService;

    @GetMapping
    @ApiOperation("条件分页查询设备操作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页大小", defaultValue = "10", dataType = "int")
    })
    public MsgResult<PageResult<List<DeviceOperationRecordVO>>> search(DeviceOperationSearchRequestVO requestVO,
                                                                       @RequestParam(defaultValue = SysConstant.DEFAULT_PAGENO) Integer pageNo,
                                                                       @RequestParam(defaultValue = SysConstant.DEFAULT_PAGESIZE) Integer pageSize){
        return this.successHandler(deviceSyncMsgService.search(requestVO, pageNo, pageSize));
    }

    @GetMapping("/{msgPid}")
    @ApiOperation("查看详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "msgPid", value = "设备操作记录的id", required = true, dataType = "int")
    })
    public MsgResult<DeviceOperationDetailsVO> details(@PathVariable Integer msgPid) {
        return this.successHandler(deviceSyncMsgService.listDetails(msgPid));
    }

    @UserOperatePower
    @PostMapping("/listing")
    @ApiOperation("挂牌")
    public MsgResult listing(@RequestBody DeviceListingRequestVO requestVO) {
        deviceService.listing(requestVO);
        return this.successHandler("挂牌成功");
    }

    @PostMapping("/listing/status")
    @ApiOperation("设备挂牌状态")
    public MsgResult<List<ListingVO>> listing(@RequestBody DeviceCodesRequestVO deviceCodes) {
        List<ListingVO> result = deviceService.getListingStatus(deviceCodes.getDeviceCodes());
        return this.successHandler(result);
    }

}
